home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ExampleFileView.java < prev    next >
Text File  |  1998-09-28  |  6KB  |  182 lines

  1. /*
  2.  * @(#)ExampleFileView.java    1.4 98/04/14
  3.  * 
  4.  * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.preview.filechooser.*;
  23. import com.sun.java.swing.preview.*;
  24.  
  25. import java.io.File;
  26. import java.util.Hashtable;
  27.  
  28. /**
  29.  * An implementation of the FileView interface that
  30.  * manages name, icon, traversable, and file type information.
  31.  *
  32.  * This implemention will work well with file systems that use
  33.  * "dot" extensions to indicate file type. For example: "picture.gif"
  34.  * as a gif image.
  35.  *
  36.  * If the java.io.File ever contains some of this information, such as
  37.  * file type, icon, and hidden file inforation, this implementation may
  38.  * become obsolete. At minimum, it should be rewritten at that time to
  39.  * use any new type information provided by java.io.File
  40.  *
  41.  * Example:
  42.  *    JFileChooser chooser = new JFileChooser();
  43.  *    fileView = new ExampleFileView();
  44.  *    fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
  45.  *    fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
  46.  *    chooser.setFileView(fileView);
  47.  
  48.  */
  49. public class ExampleFileView extends FileView {
  50.     private Hashtable icons = new Hashtable(5);
  51.     private Hashtable fileDescriptions = new Hashtable(5);
  52.     private Hashtable typeDescriptions = new Hashtable(5);
  53.     
  54.     /**
  55.      * The name of the file.  Do nothing special here. Let
  56.      * the system file view handle this.
  57.      * @see #setName
  58.      * @see FileView#getName
  59.      */
  60.     public String getName(File f) {
  61.     return null;
  62.     }
  63.  
  64.     /**
  65.      * Adds a human readable description of the file.
  66.      */
  67.     public void putDescription(File f, String fileDescription) {
  68.     fileDescriptions.put(fileDescription, f);
  69.     }
  70.  
  71.     /**
  72.      * A human readable description of the file.
  73.      *
  74.      * @see FileView#getDescription
  75.      */
  76.     public String getDescription(File f) {
  77.     return (String) fileDescriptions.get(f);
  78.     };
  79.  
  80.     /**
  81.      * Adds a human readable type description for files. Based on "dot"
  82.      * extension strings, e.g: ".gif". Case is ignored.
  83.      */
  84.     public void putTypeDescription(String extension, String typeDescription) {
  85.     typeDescriptions.put(typeDescription, extension);
  86.     }
  87.  
  88.     /**
  89.      * Adds a human readable type description for files of the type of
  90.      * the passed in file. Based on "dot" extension strings, e.g: ".gif".
  91.      * Case is ignored.
  92.      */
  93.     public void putTypeDescription(File f, String typeDescription) {
  94.     putTypeDescription(getExtension(f), typeDescription);
  95.     }
  96.  
  97.     /**
  98.      * A human readable description of the type of the file.
  99.      *
  100.      * @see FileView#getTypeDescription
  101.      */
  102.     public String getTypeDescription(File f) {
  103.     return (String) typeDescriptions.get(getExtension(f));
  104.     }
  105.  
  106.     /**
  107.      * Conveinience method that returnsa the "dot" extension for the
  108.      * given file.
  109.      */
  110.     public String getExtension(File f) {
  111.     String name = f.getName();
  112.     if(name != null) {
  113.         int extensionIndex = name.lastIndexOf('.');
  114.         if(extensionIndex < 0) {
  115.         return null;
  116.         }
  117.         return name.substring(extensionIndex+1).toLowerCase();
  118.     } 
  119.     return null;
  120.     }
  121.  
  122.     /**
  123.      * Adds an icon based on the file type "dot" extension
  124.      * string, e.g: ".gif". Case is ignored.
  125.      */
  126.     public void putIcon(String extension, Icon icon) {
  127.     icons.put(extension, icon);
  128.     }
  129.  
  130.     /**
  131.      * Icon that reperesents this file. Default implementation returns
  132.      * null. You might want to override this to return something more
  133.      * interesting.
  134.      *
  135.      * @see FileView#getIcon
  136.      */
  137.     public Icon getIcon(File f) {
  138.     Icon icon = null;
  139.     String extension = getExtension(f);
  140.     if(extension != null) {
  141.         icon = (Icon) icons.get(extension);
  142.     }
  143.     return icon;
  144.     }
  145.  
  146.     /**
  147.      * Whether the file is hidden or not. This implementation returns
  148.      * true if the filename starts with a "."
  149.      *
  150.      * @see FileView#isHidden
  151.      */
  152.     public Boolean isHidden(File f) {
  153.     String name = f.getName();
  154.     if(name != null && !name.equals("") && name.charAt(0) == '.') {
  155.         return Boolean.TRUE;
  156.     } else {
  157.         return Boolean.FALSE;
  158.     }
  159.     };
  160.  
  161.     /**
  162.      * Whether the directory is traversable or not. Generic implementation
  163.      * returns true for all directories.
  164.      *
  165.      * You might want to subtype ExampleFileView to do somethimg more interesting,
  166.      * such as recognize compound documents directories; in such a case you might
  167.      * return a special icon for the diretory that makes it look like a regular
  168.      * document, and return false for isTraversable to not allow users to
  169.      * descend into the directory.
  170.      *
  171.      * @see FileView#isTraversable
  172.      */
  173.     public Boolean isTraversable(File f) {
  174.     if(f.isDirectory()) {
  175.         return Boolean.TRUE;
  176.     } else {
  177.         return Boolean.FALSE;
  178.     }
  179.     };
  180.  
  181. }
  182.